package com.ruoyi.prevention.worker.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.prevention.segregater.domain.Segregater;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.prevention.worker.domain.Worker;
import com.ruoyi.prevention.worker.service.IWorkerService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 检测人员信息Controller
 * 
 * @author wad
 * @date 2022-04-06
 */
@RestController
@RequestMapping("/prevention/worker")
public class WorkerController extends BaseController
{
    @Autowired
    private IWorkerService workerService;
    @Autowired
    private TokenService tokenService;

    /**
     * 查询检测人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('prevention:worker:list')")
    @GetMapping("/list")
    public TableDataInfo list(Worker worker)
    {
        startPage();
        List<Worker> list = workerService.selectWorkerList(worker);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('prevention:worker:list')")
    @GetMapping("/workerList")
    public List<Worker> workerList(Worker worker)
    {
        List<Worker> list = workerService.selectWorkerList(worker);
        return list;
    }


    /**
     * 导出检测人员信息列表
     */
    @PreAuthorize("@ss.hasPermi('prevention:worker:export')")
    @Log(title = "检测人员信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Worker worker)
    {
        List<Worker> list = workerService.selectWorkerList(worker);
        ExcelUtil<Worker> util = new ExcelUtil<Worker>(Worker.class);
        util.exportExcel(response, list, "检测人员信息数据");
    }

    /**
     * 获取检测人员信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('prevention:worker:query')")
    @GetMapping(value = "/{workerId}")
    public AjaxResult getInfo(@PathVariable("workerId") Long workerId)
    {
        return AjaxResult.success(workerService.selectWorkerByWorkerId(workerId));
    }

    /**
     * 新增检测人员信息
     */
    @PreAuthorize("@ss.hasPermi('prevention:worker:add')")
    @Log(title = "检测人员信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Worker worker)
    {
        return toAjax(workerService.insertWorker(worker));
    }

    /**
     * 修改检测人员信息
     */
    @PreAuthorize("@ss.hasPermi('prevention:worker:edit')")
    @Log(title = "检测人员信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Worker worker)
    {
        return toAjax(workerService.updateWorker(worker));
    }

    /**
     * 删除检测人员信息
     */
    @PreAuthorize("@ss.hasPermi('prevention:worker:remove')")
    @Log(title = "检测人员信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{workerIds}")
    public AjaxResult remove(@PathVariable Long[] workerIds)
    {
        return toAjax(workerService.deleteWorkerByWorkerIds(workerIds));
    }

    @Log(title = "隔离人员信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('prevention:worker:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<Worker> util = new ExcelUtil<Worker>(Worker.class);
        List<Worker> userList = util.importExcel(file.getInputStream());
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        String operName = loginUser.getUsername();
        String message = workerService.importUser(userList, updateSupport, operName);
        return AjaxResult.success(message);
    }

    /**
     * 下载模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response){
        ExcelUtil<Worker> util = new ExcelUtil<Worker>(Worker.class);
        util.importTemplateExcel(response, "用户数据");
    }
}
